package cn.vinann.demo.controller;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.vinann.demo.common.TokenUtils;

@Controller
public class LoginController {

	@Autowired
	private TokenUtils tokenUtils;
	
	@Value("${token.header}")
    private String tokenHeader;
	
	@RequestMapping("/login")
	public String login(String username,String password,HttpServletResponse response){
		
		//1.验证用户名面是否正确
		//2.生成token
		String token = tokenUtils.generateToken(username);
		Cookie cookie = new Cookie(tokenHeader, token);
		cookie.setMaxAge(900);
		response.addCookie(cookie);
		return "redirect:/home/index";
	}
	
	@RequestMapping("/home/index")
	public String homeIndex(){
		
		return "home";
	}
}
